/*
    Space Simulation - Simulate the solar system
    Copyright (C) 2010  Guillaume Andrieu and Sylvain Martin

    This file is part of Space Simulation.

    Space Simulation is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Space Simulation is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Space Simulation.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
 *Simulation.c contiens les fonctions utiles a créer et "lancer" une simulation.
 *	MakeSimulation : fonction qui crée une "simulation", structure dont on rempli les differents champs
 *	updateSimulation : fonction qui rafraichis l'affichage de la "simulation" en fonction du temps
 *	addParticule	 : fonction qui ajoute une particule crée dans la simulation voulue.
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Vector3D.h"
#include "Particule.h"
#include "Simulation.h"
#include "physic.h"

Simulation MakeSimulation(Space space, time_t start_time, double time_step, double speed)
{
  Simulation s;
  s.space = space;
  s.particules = NULL;
  s.particules_num = 0;
  s.start_time = start_time;
  s.time_step = time_step;
  s.speed = speed;
  return s;
}

void updateSimulation(Simulation * s, double exec_time)
{
  double time;
  for(time = s->space.time + s->time_step; time <= exec_time * s->speed; time += s->time_step)
  {
    dynamic_calculation(s, time);
  }
}

void addParticule(Simulation * s, Particule p)
{
  if(s->particules == NULL)
  {
    s->particules_num = 1;
    s->particules = malloc(1 * sizeof(Particule));
  }
  else
  {
    s->particules_num++,
    s->particules = realloc(s->particules, s->particules_num * sizeof(Particule));
  }
  s->particules[s->particules_num-1] = p;
}
